package com.example.findtherinbow;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.findtherinbow.utils.GsonUtil;
import com.example.findtherinbow.utils.SharedPreferencesUtil;
import com.google.gson.Gson;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;

public class PlayerActivity extends AppCompatActivity {

    public static final String PLAYER_SCORE = "PLAYER_SCORE";
    public static final String SAVE_SCORE_RECORDS = "SAVE_SCORE_RECORDS";
    private TextView tvPlayerScore;
    private EditText etPlayerGame;
    private RadioGroup rgAvatar;
    private int playerScore;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_player);

        initView();


    }

    private void initView() {
        playerScore = getIntent().getIntExtra(PLAYER_SCORE, -1);

        tvPlayerScore = findViewById(R.id.tv_player_score);
        etPlayerGame = findViewById(R.id.et_player_game);
        rgAvatar = findViewById(R.id.rg_avatar);

        tvPlayerScore.setText(String.format(getString(R.string.get_score), String.valueOf(playerScore)));


    }


    private String selectPlayer() {
        int selectedRadioButtonId = rgAvatar.getCheckedRadioButtonId();

        if (selectedRadioButtonId != -1) {
            RadioButton selectedRadioButton = findViewById(selectedRadioButtonId);

            return (String) selectedRadioButton.getTag();
        }

        return null;
    }

    public void onclickSubmit(View view) {
        String playerName = etPlayerGame.getText().toString().trim();
        if (TextUtils.isEmpty(playerName)) {
            Toast.makeText(this, getString(R.string.player_tips), Toast.LENGTH_SHORT).show();
            return;
        }

        String playerAvatar = selectPlayer();
        if (playerAvatar == null) {
            Toast.makeText(this, getString(R.string.player_tips), Toast.LENGTH_SHORT).show();
            return;
        }


        updateLeaderboard(playerName, playerAvatar);

        startActivity(new Intent(PlayerActivity.this, LeaderBoardActivity.class));
        finish();
    }


    private void updateLeaderboard(String playerName, String playerAvatar) {
        String scoreRecords = SharedPreferencesUtil.getString(PlayerActivity.this, SAVE_SCORE_RECORDS, null);


        if (scoreRecords == null) {
            List<Player> list = new ArrayList<>();
            Player player = new Player(playerName, playerAvatar, playerScore);
            list.add(player);


            String json = GsonUtil.getInstance().toJson(list);
            SharedPreferencesUtil.saveString(PlayerActivity.this, SAVE_SCORE_RECORDS, json);
        } else {
            boolean isNew = true;
            List<Player> list = GsonUtil.getInstance().jsonToList(scoreRecords, Player.class);
            String drawable = selectPlayer();

            for (Player player : list) {
                if (Objects.equals(drawable, player.getPlayerAvatar())) {
                    if (playerScore > player.getPlayerScore()) {
                        player.setPlayerName(playerName);
                        player.setPlayerScore(playerScore);
                    }
                    isNew = false;
                }
            }

            if (isNew) {
                list.add(new Player(playerName, playerAvatar, playerScore));
            }

            // 使用 Collections 类的 sort 方法进行排序
            Collections.sort(list, new Comparator<Player>() {
                @Override
                public int compare(Player player1, Player player2) {
                    // 按照 playerScore 降序排序
                    return Integer.compare(player2.getPlayerScore(), player1.getPlayerScore());
                }
            });

            String json = GsonUtil.getInstance().toJson(list);
            SharedPreferencesUtil.saveString(PlayerActivity.this, SAVE_SCORE_RECORDS, json);
        }
    }

}